<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 节点控制器
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class NodeController extends InitController{

   /**
    * 数据查询过滤
    */
   public function filter_( &$arrMap ){

   	    $arrMap['node_name'] = array( 'like',"%".Safe::getGpc( 'node_name' )."%" );

        $nGroupId = Safe::getGpc( 'group_id','G' );
        $sSearch = Safe::getGpc( 'search','P' );
        $nNodeParentid = Safe::getGpc( 'node_parentid','G' );

        if( $nGroupId===null ) $nGroupId =0;

        if( !empty( $nGroupId ) ) {

            $arrMap['group_id'] =  &$nGroupId;
            $this->assign( 'sNodeName', G::L( '分组' ) );
        }

        else if( empty( $sSearch ) && !isset( $arrMap['node_parentid'] ) ) {

			$arrMap['node_parentid']	=	0;
		}

		if( !empty( $nNodeParentid ) ){

			$arrMap['node_parentid'] = &$nNodeParentid;
		}

		$this->assign( 'nGroupId',$nGroupId );

		// 保存COOKIE
		$this->setCookieData( 'current_node_id',$nNodeParentid );

		// 获取上级节点
        if( isset($arrMap['node_parentid'] ) ) {

            if( $oNode = NodeModel::F()->getBynode_id( $arrMap['node_parentid'] ) ) {

                $this->assign('nNodeLevel',$oNode->node_level+1);
                $this->assign('sNodeName',$oNode->node_name);
            }

            else {

                $this->assign( 'nNodeLevel',1 );
            }
        }

        else{

        	$this->assign( 'nNodeLevel',1 );
        }
   }

   /**
    * 获取权限
    */
   public function get_access( ){

   	  $sAccess = Safe::getGpc( 'access','P');
   	  $nNodeLevel=1;

   	  if( $sAccess == 'app' ){

   	  	  $arrAccessList = NodeModel::F()
   	  	                   ->where( array('node_level'=>1,'node_parentid'=>0) )
   	  	                   ->asArray()
   	  	                   ->all( )
   	  	                   ->query();

   	  	  $nNodeLevel = 2;
   	  }

   	  elseif( $sAccess == 'module' ){

   	  	  $arrAccessList = NodeModel::F()
   	  	                   ->where( array('node_level'=>2) )
   	  	                   ->asArray()
   	  	                   ->all()
   	  	                   ->query();

   	  	  $nNodeLevel = 3;
   	  }

   	  $this->assign( 'arrAccessList',$arrAccessList );
   	  $this->assign( 'nNodeLevel',$nNodeLevel );
   	  $this->display();
   }

   /**
    * 获取分组列表
    */
   public function getGroup(){

   	   $arrGroup = array_merge(

   	         array(  array('group_id'=>0,'group_title'=>G::L( '未分组' ) ) ),
   	         GroupModel::F()
   	                   ->setColumns( 'group_id,group_title' )
   	                   ->asArray()
   	                   ->all()
   	                   ->query()
   	   );

   	   $this->assign( 'arrGroup',$arrGroup );
   }

   /**
    * 数据列表执行前处理
    */
   public function bIndex_( ){

   	   $this->getGroup( );
   }

   /**
    * 增加数据前处理
    */
   public function bAdd_( ){

   	   $this->getGroup();
   }

   /**
    * 编辑数据前处理
    */
   public function bEdit_(){

   	   $this->getGroup();
   }

   /**
    * 节点排序
    */
   public function sort(){

	    $nSortId = Safe::getGpc( 'sort_id' );

        if( !empty( $nSortId ) ) {

            $arrMap['node_status'] = 1;
            $arrMap['node_id']   = array( 'in',$nSortId );
            $arrSortList  = NodeModel::F()
                            ->order('node_sort ASC')
                            ->asArray()
                            ->all()
                            ->where( $arrMap )
                            ->query( );
        }

        else{

        	$nNodeParentid = Safe::getGpc( 'node_parentid' );

            if( !empty( $nNodeParentid ) ) {

                $nPid  = &$nNodeParentid;
            }

            else {

                $nPid  = $this->getCookieData( 'current_node_id',$nNodeParentid );;
            }

            if( $nPid===null ) $nPid =0;

            $arrNode = NodeModel::F()->getBynode_id( $nPid )->toArray();
            if( isset( $arrNode['node_id'] ) ) {

                $nLevel   =  $arrNode['node_level']+1;
            }

            else {

                $nLevel   =  1;
            }

            $this->assign( 'nLevel',$nLevel );

            $arrSortList  =  NodeModel::F()
                             ->where( 'node_status=1 and node_parentid=? and node_level=?',$nPid,$nLevel )
                             ->order( 'node_sort ASC' )
                             ->asArray()
                             ->all( )
                             ->query();

        }

        $this->assign( "arrSortList",$arrSortList );
        $this->display();
    }

}
